package com.yanqu.road.server.manager.union.honors;

import com.yanqu.road.entity.rank.UnionValueRank;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossGroupUnionValueRankListModel;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.utils.ConfigHelper;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionHonorsRankMgr extends TempMgr {

    static Map<Integer, CrossGroupUnionValueRankListModel> groupRankMap;

    @Override
    public boolean init() throws Exception {
        ServerGroupMgr.addReloadListener(() -> unionGroupReload());
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {


        return true;
    }

    @Override
    public boolean reloadData() throws Exception {

        initGroupRankMap();
        return true;
    }

    private void initGroupRankMap() {

        Map<Integer, LinkedList<UnionValueRank>> data = CrossUnionHonorsMgr.buildGroupRankData();

        Map<Integer, CrossGroupUnionValueRankListModel> groupRankMapTemp = new ConcurrentHashMap<>();

        for (Map.Entry<Integer, LinkedList<UnionValueRank>> listEntry : data.entrySet()) {
            CrossGroupUnionValueRankListModel model = new CrossGroupUnionValueRankListModel(listEntry.getKey());
            model.setRankNum(GameConfig.UNION_STORY_RANK_NUM);
            model.setRankList(listEntry.getValue());
            groupRankMapTemp.put(listEntry.getKey(), model);
        }

        groupRankMap = groupRankMapTemp;
    }

    /**
     * 更新榜单
     */
    public static void changeUnionRank(long serverId, UnionValueRank unionRank) {
        int groupId = ServerGroupMgr.getGroupIdByServerId(serverId);

        CrossGroupUnionValueRankListModel rankListModel = getCrossGroupUnionRankListModel(groupId);
        if(rankListModel == null){
            synchronized (groupRankMap){
                rankListModel = getCrossGroupUnionRankListModel(groupId);
                if(rankListModel == null){
                    rankListModel = new CrossGroupUnionValueRankListModel(groupId);
                    groupRankMap.put(groupId, rankListModel);
                }
            }
            rankListModel = getCrossGroupUnionRankListModel(groupId);
        }
        if(rankListModel != null){
            rankListModel.rankChange(unionRank);
        }
    }

    public static CrossGroupUnionValueRankListModel getCrossGroupUnionRankListModel(int groupId){
        return groupRankMap.get(groupId);
    }

    /**
     * 榜单重新分组
     */
    private void unionGroupReload() {

        int channelId = ConfigHelper.getInt("channelId");
        if(channelId == 0){
            return;
        }

        Map<Integer, CrossGroupUnionValueRankListModel> oldGroupRankMapTemp = groupRankMap;
        groupRankMap = new ConcurrentHashMap<>();

        Map<Integer, CrossGroupUnionValueRankListModel> newGroupRankMapTemp = new ConcurrentHashMap<>();
        Map<Integer, ServerGroup> groupMap = ServerGroupMgr.getServerGroupMap(channelId);
        for (Map.Entry<Integer, ServerGroup> entry : groupMap.entrySet()) {
            CrossGroupUnionValueRankListModel model = new CrossGroupUnionValueRankListModel(entry.getKey());
            model.setRankNum(GameConfig.UNION_STORY_RANK_NUM);
            model.setRankList(new LinkedList<>());
            newGroupRankMapTemp.put(entry.getKey(), model);
        }

        for (Map.Entry<Integer, CrossGroupUnionValueRankListModel> modelEntry : oldGroupRankMapTemp.entrySet()) {
            CrossGroupUnionValueRankListModel listModel = modelEntry.getValue();
            for (UnionValueRank rank : listModel.getRankList()) {
                long serverId = rank.getUnionBaseInfo().getServerId();
                int groupId = ServerGroupMgr.getGroupIdByServerId(serverId);
                CrossGroupUnionValueRankListModel model = newGroupRankMapTemp.get(groupId);
                if(model != null){
                    model.rankChange(rank);
                }
            }
        }

        groupRankMap = newGroupRankMapTemp;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
